'Filename : Advance Registry.cls (cAdvanceRegistry Class Module)
'Description : Advance registry functions for VB programmers
'Date : Wednesday, February 2, 2005, 11:02 PM
'Last Update : Thursday, November 24, 2005, 02:14 PM
'You can freely use and distribute this class or upload these codes on any site
'provided that the original credits are kept unmodified.
'User Caution :
'Incorrectly editing the registry may severely damage your system.
'Before using this module, you should back up any valued data on your computer.
'Keep note that :
'If the remote registry is on a system that is running Windows NT 4.0, 2000, or XP,
'you must run the code from an account that has permission to read that registry.
'Be carefull in using the DeleteTree function. Backup your registry for more assurance.
'The author will not be held liable for any damages due to improper use of this module.
'Binary typed registry data are returned in an array of ascii numbers depending on how
'the data is written in the registry. Binary in unicode format can be read as strings.
'Most arguments are on a variant type (sorry???)...
'IsMissing command only works for variant type variables, so I decided to use variants.
'Credits goes to:
'Makers of the great Win32 Programmer's Reference, don't know who you are but thanks.
'Christoph von Wittich (Christoph@ActiveVB.de), author of ApiViewer 2004 for the APIs
'Sample usage :
'Dim KeyArray() As String
'If REG.EnumerateKeys(KeyArray, HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows") Then
' For i = LBound(KeyArray) To UBound(KeyArray)
' Debug.Print KeyArray(i)
' Next i
'End If
'API declarations
Private Declare Function ExpandEnvironmentStrings Lib "kernel32.dll" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hKey As Long, ByRef phkResult As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByRef lpData As Any, ByRef lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Any, ByVal cbData As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Modified API declarations
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Long, ByRef Source As Long, ByVal Length As Long)
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, ByRef lpcbValueName As Long, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As Byte, ByVal lpcbData As Long) As Long
Private Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, ByRef lpcbClass As Long, ByVal lpReserved As Long, ByRef lpcSubKeys As Long, ByRef lpcbMaxSubKeyLen As Long, ByRef lpcbMaxClassLen As Long, ByRef lpcValues As Long, ByRef lpcbMaxValueNameLen As Long, ByRef lpcbMaxValueLen As Long, ByRef lpcbSecurityDescriptor As Long, ByRef lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegQueryValueExByte Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal lKey As Long, ByVal lpValueName As String, ByVal lReserved As Long, ByRef lpType As Long, ByRef lpData As Byte, ByRef lpcbData As Long) As Long
Private Declare Function RegQueryValueExDouble Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal lKey As Long, ByVal lpValueName As String, ByVal lReserved As Long, ByRef lpType As Long, ByRef lpData As Double, ByRef lpcbData As Long) As Long
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal lKey As Long, ByVal lpValueName As String, ByVal lReserved As Long, ByRef lpType As Long, ByRef lpData As Long, ByRef lpcbData As Long) As Long
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal lKey As Long, ByVal lpValueName As String, ByVal lReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegSetValueExByte Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Byte, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExDouble Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Double, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
'Registry security option constants
Private Const KEY_CREATE_LINK As Long = &H20
Private Const KEY_CREATE_SUB_KEY As Long = &H4
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Private Const KEY_NOTIFY As Long = &H10
Private Const KEY_QUERY_VALUE As Long = &H1
Private Const KEY_SET_VALUE As Long = &H2
Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000
Private Const SYNCHRONIZE As Long = &H100000
Private Const KEY_ALL_ACCESS As Long = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
'Registry create type values
Private Const REG_OPTION_NON_VOLATILE As Long = 0 'Key is preserved on system reboot
Private Const REG_OPTION_RESERVED As Long = 0 'Parameter is reserved for future use
'Registry create/open disposition
Private Const REG_CREATED_NEW_KEY As Long = &H1 'New registry key was created
Private Const MAX_CLASS_NAME As Long = 255
'Registry return constants
Private Const ERROR_ACCESS_DENIED As Long = 5& 'Access to registry key was denied
Private Const ERROR_MORE_DATA As Long = 234 'More data is available
Private Const ERROR_NO_MORE_ITEMS As Long = 259& 'No data is available
Private Const ERROR_SUCCESS As Long = 0& 'Operation was completed successfully
'Other constaints
Private Const CLASS_NAME As String = ".cAdvanceRegistry"
'API types
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
'Public enums
Enum ERegistryRoots
HKEY_CLASSES_ROOT = &H80000000 'Contains file association mappings
HKEY_CURRENT_USER = &H80000001 'Contains information about the current user
HKEY_LOCAL_MACHINE = &H80000002 'Contains computer specific information
HKEY_USERS = &H80000003 'Contains individual preferences for each users
End Enum
#If False Then
'Trick to preserve casing of these variables when used in VB IDE
Err.Raise m_DataType, App.EXEName & CLASS_NAME, "Cannot set the specified value in the registry using the defined data type."
End If
Case Else
Err.Raise m_DataType, App.EXEName & CLASS_NAME, "The specified data type is either invalid or not supported for write purpose."
End Select
Call RegCloseKey(p_Key)
Else
Err.Raise ClassRoot, App.EXEName & CLASS_NAME, "Unable to open/create registry key: '" & Key & "' for setting new value of '" & ValueName & "' to '" & Value & "'"
End If
End Property
Property Get ValueName() As String
Attribute ValueName.VB_Description = "Returns/sets a name to identify a particular registry data."
'Returns/sets a name to identify a particular registry data.
ValueName = m_ValueName
End Property
Property Let ValueName(Value As String)
m_ValueName = Value
End Property
'Public procedures
Function CreateKey(Optional ClassRoot, Optional Key) As Boolean
Attribute CreateKey.VB_Description = "Creates a new registry key from a specified path of a registry root."
'Creates a new registry key from a specified path of a registry root.